<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业实现低成本数字化转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
namespace app\controller\api\items\common;

use app\controller\api\Base;
use api\sms\exception\SmsException;
use app\common\services\SmsService;
use think\captcha\facade\Captcha;

class Code extends Base
{

    /**
     *生成图形验证码
     * @return mixed|\think\Response
     *
     * @author Amos
     * 2024-06-12 11:02
     */
    public function createVerifyCode()
    {
        $imgData = Captcha::create();
        if ((int)$this->request->param('is_img') === 1) {
            return $imgData;
        }
        return $this->success('data:image/jpg;base64,' . base64_encode($imgData->getContent()));
    }


    /**
     * 发送手机验证码
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function sendPhoneVerifyCode()
    {
        $phone = $this->request->param('mobile', '', 'trim');
        $type = $this->request->param('type', '', 'intval');
        try {
            SmsService::init($this->request->itemsId)->sendVerifyCode($phone, $type);
        } catch (SmsException $e) {
            if ($e->getCode() == 1001) {
                return $this->error($e->getMessage(), 400);
            } else {
                return $this->error($e->getMessage());
            }
        }
        return $this->successText('发送成功');
    }
}